home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / handson / java / vectors / frame1.java < prev    next >
Encoding:
Java Source  |  1997-02-07  |  6.4 KB  |  213 lines

  1. /*
  2.     PC Plus sample Java application.
  3.     Demonstrates how to transfer items between two Vectors by
  4.     boxes by double-clicking List boxes. Implements a basic
  5.     'shopping list' manager.
  6.  */
  7.  
  8. import java.awt.*;
  9. import java.util.*; // Class Vector is in java.util
  10.  
  11. public class Frame1 extends Frame {
  12.  
  13.     // Declare two Vectors to main a stock list and a list of items purchased
  14.     Vector thingsInStock;
  15.     Vector thingsInBasket;
  16.     // Note, these Vectors are not actually available for use until they are
  17.     // created with 'new'. In this project, this is done in the Frame's 'show'
  18.     // method. However, the Vectors could have been created at the time of declaration
  19.     // using the following statements instead of the declarations above:
  20.     //         Vector thingsInStock = new Vector();
  21.     //         Vector thingsInBasket = new Vector();
  22.  
  23.  
  24.     void MoveObFromV1ToV2(Object Ob, Vector V1, Vector V2 ) {
  25.         // Move an item from Vector V1 to Vector V2
  26.         V2.addElement(Ob);
  27.         V1.removeElement(Ob);
  28.     }
  29.  
  30.  
  31.     void BasketList_DblClick(Event event) {
  32.         // transfer selected item from thingsInBasket vector to thingsInStock vector
  33.         // and display the change in the list boxes
  34.         Object thing = thingsInBasket.elementAt(BasketList.getSelectedIndex());
  35.         MoveObFromV1ToV2(thing, thingsInBasket, thingsInStock );
  36.         UpdateListBoxes();
  37.  
  38.     }
  39.  
  40.     void StockList_DblClick(Event event) {
  41.         // transfer selected item from thingsInStock vector to thingsInBasket vector
  42.         // and display the change in the list boxes
  43.         Object thing = thingsInStock.elementAt(StockList.getSelectedIndex());
  44.         MoveObFromV1ToV2(thing, thingsInStock, thingsInBasket );
  45.         UpdateListBoxes();
  46.     }
  47.  
  48.     void UpdateListBox( List l, Vector v) {
  49.         // Enumerate through the elements in Vector v and add them,
  50.         // as strings, to the List l
  51.         l.clear();
  52.         for (Enumeration e = v.elements(); e.hasMoreElements(); ) {
  53.             l.addItem(e.nextElement().toString());
  54.         }
  55.     }
  56.  
  57.     void UpdateListBoxes() {
  58.         // Call UpdateListBox with the Lists and the Vectors representing
  59.         // the things in the Basket and the things in Stock
  60.         UpdateListBox(BasketList, thingsInBasket);
  61.         UpdateListBox(StockList, thingsInStock);
  62.     }
  63.  
  64.     void Open_Action(Event event) {
  65.         OpenFileDialog.show();
  66.     }
  67.  
  68.     void About_Action(Event event) {
  69.         (new AboutDialog(this, "About...", false)).show();
  70.     }
  71.  
  72.     void Exit_Action(Event event) {
  73.         (new QuitDialog(this, "Quit the Application?", false)).show();
  74.     }
  75.  
  76.     public Frame1() {
  77.  
  78.         //{{INIT_CONTROLS
  79.         setLayout(null);
  80.         addNotify();
  81.         resize(insets().left + insets().right + 457,insets().top + insets().bottom + 322);
  82.         OpenFileDialog = new java.awt.FileDialog(this, "Open",FileDialog.LOAD);
  83.         StockList = new java.awt.List(0,false);
  84.         add(StockList);
  85.         StockList.reshape(insets().left + 14,insets().top + 45,201,225);
  86.         BasketList = new java.awt.List(0,false);
  87.         add(BasketList);
  88.         BasketList.reshape(insets().left + 238,insets().top + 45,201,225);
  89.         label1 = new java.awt.Label("ITEMS IN STOCK");
  90.         label1.reshape(insets().left + 14,insets().top + 15,196,15);
  91.         add(label1);
  92.         label2 = new java.awt.Label("ITEMS IN YOUR BASKET");
  93.         label2.reshape(insets().left + 238,insets().top + 15,203,15);
  94.         add(label2);
  95.         label3 = new java.awt.Label("DOUBLE-CLICK TO BUY");
  96.         label3.reshape(insets().left + 14,insets().top + 278,203,22);
  97.         add(label3);
  98.         label4 = new java.awt.Label("DOUBLE-CLICK TO PUT BACK");
  99.         label4.reshape(insets().left + 238,insets().top + 278,203,24);
  100.         add(label4);
  101.         setTitle("A Basic Application");
  102.         //}}
  103.  
  104.         //{{INIT_MENUS
  105.         mainMenuBar = new java.awt.MenuBar();
  106.  
  107.         menu1 = new java.awt.Menu("File");
  108.         menu1.add("Open...");
  109.         menu1.add("Save");
  110.         menu1.add("Save As...");
  111.         menu1.addSeparator();
  112.         menu1.add("Exit");
  113.         mainMenuBar.add(menu1);
  114.  
  115.         menu2 = new java.awt.Menu("Edit");
  116.         menu2.add("Cut");
  117.         menu2.add("Copy");
  118.         menu2.add("Paste");
  119.         mainMenuBar.add(menu2);
  120.  
  121.         menu3 = new java.awt.Menu("Help");
  122.         menu3.add("About");
  123.         mainMenuBar.add(menu3);
  124.         setMenuBar(mainMenuBar);
  125.         //}}
  126.     }
  127.  
  128.     public Frame1(String title) {
  129.         this();
  130.         setTitle(title);
  131.     }
  132.  
  133.     public synchronized void show() {
  134.         move(50, 50);
  135.         super.show();
  136.  
  137.         // INITIALISATION - runs when form is first shown
  138.         // Create the two Vectors
  139.         thingsInStock = new Vector();
  140.         thingsInBasket = new Vector();
  141.  
  142.         // Add items to thingsInStock Vector
  143.         thingsInStock.addElement("Tin of Schpamm");
  144.         thingsInStock.addElement("Pot of Schnoodles");
  145.         thingsInStock.addElement("Jug of Hare");
  146.         thingsInStock.addElement("Kettle of Fish");
  147.         thingsInStock.addElement("Bombay Duck");
  148.         thingsInStock.addElement("Duck-billed platypus");
  149.         thingsInStock.addElement("Can of worms");
  150.         thingsInStock.addElement("Hair of the dog");
  151.         thingsInStock.addElement("Dog's bone");
  152.         thingsInStock.addElement("PC Plus");
  153.         // Show the items in List box(es) on screen
  154.         UpdateListBoxes();
  155.     }
  156.  
  157.     public boolean handleEvent(Event event) {
  158.         if (event.id == Event.WINDOW_DESTROY) {
  159.             hide();         // hide the Frame
  160.             dispose();
  161.             System.exit(0);
  162.             return true;
  163.         }
  164.         if (event.target == StockList && event.id == Event.ACTION_EVENT) {
  165.             StockList_DblClick(event);
  166.         }
  167.         if (event.target == BasketList && event.id == Event.ACTION_EVENT) {
  168.             BasketList_DblClick(event);
  169.         }
  170.         return super.handleEvent(event);
  171.     }
  172.  
  173.     public boolean action(Event event, Object arg) {
  174.         if (event.target instanceof MenuItem) {
  175.             String label = (String) arg;
  176.             if (label.equalsIgnoreCase("Open...")) {
  177.                 Open_Action(event);
  178.                 return true;
  179.             } else
  180.             if (label.equalsIgnoreCase("About")) {
  181.                 About_Action(event);
  182.                 return true;
  183.             } else
  184.             if (label.equalsIgnoreCase("Exit")) {
  185.                 Exit_Action(event);
  186.                 return true;
  187.             }
  188.         }
  189.         return super.action(event, arg);
  190.     }
  191.  
  192.     static public void main(String args[]) {
  193.         (new Frame1()).show();
  194.     }
  195.  
  196.     //{{DECLARE_CONTROLS
  197.     java.awt.FileDialog OpenFileDialog;
  198.     java.awt.List StockList;
  199.     java.awt.List BasketList;
  200.     java.awt.Label label1;
  201.     java.awt.Label label2;
  202.     java.awt.Label label3;
  203.     java.awt.Label label4;
  204.     //}}
  205.  
  206.     //{{DECLARE_MENUS
  207.     java.awt.MenuBar mainMenuBar;
  208.     java.awt.Menu menu1;
  209.     java.awt.Menu menu2;
  210.     java.awt.Menu menu3;
  211.     //}}
  212. }
  213.